package com.ruoyi.web.controller.course;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysUnit;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 课程单元Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/course/unit")
public class UnitController extends BaseController
{
    @Autowired
    private ISysUnitService unitService;

    /**
     * 查询单元列表
     */
    @PreAuthorize("@ss.hasPermi('course:unit:list')")
    @GetMapping("/list")
    public AjaxResult list(SysUnit unit)
    {
        List<SysUnit> list = unitService.selectList(unit);
        return success(list);
    }

    /**
     * 导出单元列表
     */
    @PreAuthorize("@ss.hasPermi('course:unit:export')")
    @Log(title = "课程单元", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysUnit unit)
    {
        List<SysUnit> list = unitService.selectList(unit);
        ExcelUtil<SysUnit> util = new ExcelUtil<SysUnit>(SysUnit.class);
        return util.exportExcel(list, "单元数据");
    }

    /**
     * 获取单元详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:unit:query')")
    @GetMapping(value = "/{unitId}")
    public AjaxResult getInfo(@PathVariable("unitId") Long unitId)
    {
        return success(unitService.selectUnitById(unitId));
    }

    /**
     * 新增单元
     */
    @PreAuthorize("@ss.hasPermi('course:unit:add')")
    @Log(title = "课程单元", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUnit unit)
    {
        return toAjax(unitService.insert(unit));
    }

    /**
     * 修改单元
     */
    @PreAuthorize("@ss.hasPermi('course:unit:edit')")
    @Log(title = "课程单元", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUnit unit)
    {
        return toAjax(unitService.update(unit));
    }

    /**
     * 删除单元
     */
    @PreAuthorize("@ss.hasPermi('course:unit:remove')")
    @Log(title = "课程单元", businessType = BusinessType.DELETE)
    @DeleteMapping("/{unitIds}")
    public AjaxResult remove(@PathVariable Long[] unitIds)
    {
        return toAjax(unitService.deleteByIds(unitIds));
    }
}